package neptune.web.client.services;

import java.util.ArrayList;

import neptune.web.client.WebInterface;
import neptune.web.client.model.cluster.SerializableCluster;
import neptune.web.client.model.cluster.SerializablePhysicalNode;
import neptune.web.client.model.topology.SerializableIPAddress;
import neptune.web.client.model.topology.SerializableNetworkInterface;
import neptune.web.client.view.ClusterView;
import neptune.web.client.view.ConsoleManager;
import neptune.web.client.view.ClusterView.ClusterLiveDataProvider;
import neptune.web.client.view.components.MessageWidget;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.PopupPanel;

/**
 * 
 * @author Roberto Bifulco [RobertoBifulco.it] [info@robertoBifulco.it]
 * 
 */
public class ClusterDataProvider implements ClusterLiveDataProvider 
{

	public void updatePMdata(final String pmId, final ClusterView view) 
	{
		//String message = "Loading physical machine '" + pmId + "' infos...";
		String message = "Loading physical machine infos...";
		ConsoleManager.addConsoleMessage(message, false);
		
		view.setPMIcon(pmId, ClusterView.UPDATING_ICON);
		
		ClusterService.Util.getInstance().getPhysicalNode(pmId, new AsyncCallback<SerializablePhysicalNode>() 
		{
		
			public void onSuccess(SerializablePhysicalNode sPysicalNode) 
			{
				String message = "Physical machine '" + pmId + "' infos loaded";
				ConsoleManager.addConsoleMessage(message, false);
				view.setPMIcon(pmId, ClusterView.OK_ICON);
				String id=sPysicalNode.getId();
				String nome=sPysicalNode.getName();
				String descrizione=sPysicalNode.getDescription();
				
				ArrayList<SerializableNetworkInterface> sni = new ArrayList<SerializableNetworkInterface>();
				sni = sPysicalNode.getInterfaces();
				SerializableIPAddress sIPAddress=sni.get(0).getControlAddress();
				String ipAddress = sIPAddress.getAddress();
				String CPU=String.valueOf( sPysicalNode.getCpu().getAmount() );
				String RAM=String.valueOf( sPysicalNode.getRam() );
				String HD=String.valueOf(sPysicalNode.getHd() );
				view.setPhysicalMachineInfos(id, nome, descrizione, ipAddress, CPU, RAM, HD);
			}
		
			public void onFailure(Throwable caught) 
			{
				String message = "Unable to load physical machine '" + pmId + "' infos";
				ConsoleManager.addConsoleMessage(message, true);
				view.setPMIcon(pmId, ClusterView.NOT_IN_SYNC_ICON);
			}
		});
	}

	public void updatePMsData(final ClusterView view)
	{
		String message = "Loading cluster infos...";
		final PopupPanel popup = WebInterface.showMessageDialog(
				new MessageWidget(MessageWidget.LOADING, message), false);
		ConsoleManager.addConsoleMessage(message, false);

		ClusterService.Util.getInstance().getCluster( new AsyncCallback<SerializableCluster>()
		{

			public void onSuccess(SerializableCluster result) 
			{
				popup.hide();
				SerializableCluster cluster = result;

				for (SerializablePhysicalNode node : cluster.getNodes()) {
					String ipAddr = "-";
					for (SerializableNetworkInterface netIf : node
							.getInterfaces()) {
						if (netIf.getControlAddress() != null)
							ipAddr = netIf.getControlAddress()
									.getAddress();
					}

					String nodeId = node.getId();
					String nodeName =node.getName();
					String nodeDesc=node.getDescription();
					String cpu = "-";
					String ram = "-/" + node.getRam();
					String HD = "-/" + node.getHd();
					view.addPhysicalMachine(nodeId, nodeName, nodeDesc, ipAddr, cpu, ram, HD);

					updatePMdata(nodeId, view);
				}
			}

			public void onFailure(Throwable caught) 
			{
				popup.hide();
				String message = "Unable to load Cluster infos. Cause: \n"
						+ caught.getMessage();
				WebInterface.addMessageToConsoleAndShowDialog(message,
						true);
			}
		});

	}

}
